home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / clang / gnumake.zip / AR.C < prev    next >
C/C++ Source or Header  |  1994-03-23  |  8KB  |  318 lines

  1. /* Interface to `ar' archives for GNU Make.
  2. Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
  3. This file is part of GNU Make.
  4.  
  5. GNU Make is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. GNU Make is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Make; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "make.h"
  20.  
  21. #ifndef    NO_ARCHIVES
  22.  
  23. #include "file.h"
  24. #include "dep.h"
  25. #include <fnmatch.h>
  26.  
  27. /* Defined in arscan.c.  */
  28. extern long int ar_scan ();
  29. extern int ar_member_touch ();
  30. extern int ar_name_equal ();
  31.  
  32.  
  33. /* Return nonzero if NAME is an archive-member reference, zero if not.
  34.    An archive-member reference is a name like `lib(member)'.
  35.    If a name like `lib((entry))' is used, a fatal error is signaled at
  36.    the attempt to use this unsupported feature.  */
  37.  
  38. int
  39. ar_name (name)
  40.      char *name;
  41. {
  42.   char *p = index (name, '('), *end = name + strlen (name) - 1;
  43.   
  44.   if (p == 0 || p == name || *end != ')')
  45.     return 0;
  46.  
  47.   if (p[1] == '(' && end[-1] == ')')
  48.     fatal ("attempt to use unsupported feature: `%s'", name);
  49.  
  50.   return 1;
  51. }
  52.  
  53.  
  54. /* Parse the archive-member reference NAME into the archive and member names.
  55.    Put the malloc'd archive name in *ARNAME_P if ARNAME_P is non-nil;
  56.    put the malloc'd member name in *MEMNAME_P if MEMNAME_P is non-nil.  */
  57.  
  58. void
  59. ar_parse_name (name, arname_p, memname_p)
  60.      char *name, **arname_p, **memname_p;
  61. {
  62.   char *p = index (name, '('), *end = name + strlen (name) - 1;
  63.  
  64.   if (arname_p != 0)
  65.     *arname_p = savestring (name, p - name);
  66.  
  67.   if (memname_p != 0)
  68.     *memname_p = savestring (p + 1, end - (p + 1));
  69. }  
  70.  
  71. static long int ar_member_date_1 ();
  72.  
  73. /* Return the modtime of NAME.  */
  74.  
  75. time_t
  76. ar_member_date (name)
  77.      char *name;
  78. {
  79.   char *arname;
  80.   int arname_used = 0;
  81.   char *memname;
  82.   long int val;
  83.  
  84.   ar_parse_name (name, &arname, &memname);
  85.  
  86.   /* Make sure we know the modtime of the archive itself because we are
  87.      likely to be called just before commands to remake a member are run,
  88.      and they will change the archive itself.
  89.  
  90.      But we must be careful not to enter_file the archive itself if it does
  91.      not exist, because pattern_search assumes that files found in the data
  92.      base exist or can be made.  */
  93.   {
  94.     struct file *arfile;
  95.     arfile = lookup_file (arname);
  96.     if (arfile == 0 && file_exists_p (arname))
  97.       {
  98.     arfile = enter_file (arname);
  99.     arname_used = 1;
  100.       }
  101.  
  102.     if (arfile != 0)
  103.       (void) f_mtime (arfile, 0);
  104.   }
  105.  
  106.   val = ar_scan (arname, ar_member_date_1, (long int) memname);
  107.  
  108.   if (!arname_used)
  109.     free (arname);
  110.   free (memname);
  111.  
  112.   return (val <= 0 ? (time_t) -1 : (time_t) val);
  113. }
  114.  
  115. /* This function is called by `ar_scan' to find which member to look at.  */
  116.  
  117. /* ARGSUSED */
  118. static long int
  119. ar_member_date_1 (desc, mem, truncated,
  120.           hdrpos, datapos, size, date, uid, gid, mode, name)
  121.      int desc;
  122.      char *mem;
  123.      int truncated;
  124.      long int hdrpos, datapos, size, date;
  125.      int uid, gid, mode;
  126.      char *name;
  127. {
  128.   return ar_name_equal (name, mem, truncated) ? date : 0;
  129. }
  130.  
  131. /* Set the archive-member NAME's modtime to now.  */
  132.  
  133. int
  134. ar_touch (name)
  135.      char *name;
  136. {
  137.   char *arname, *memname;
  138.   int arname_used = 0;
  139.   register int val;
  140.  
  141.   ar_parse_name (name, &arname, &memname);
  142.  
  143.   /* Make sure we know the modtime of the archive itself before we
  144.      touch the member, since this will change the archive itself.  */
  145.   {
  146.     struct file *arfile;
  147.     arfile = lookup_file (arname);
  148.     if (arfile == 0)
  149.       {
  150.     arfile = enter_file (arname);
  151.     arname_used = 1;
  152.       }
  153.  
  154.     (void) f_mtime (arfile, 0);
  155.   }
  156.  
  157.   val = 1;
  158.   switch (ar_member_touch (arname, memname))
  159.     {
  160.     case -1:
  161.       error ("touch: Archive `%s' does not exist", arname);
  162.       break;
  163.     case -2:
  164.       error ("touch: `%s' is not a valid archive", arname);
  165.       break;
  166.     case -3:
  167.       perror_with_name ("touch: ", arname);
  168.       break;
  169.     case 1:
  170.       error ("touch: Member `%s' does not exist in `%s'", memname, arname);
  171.       break;
  172.     case 0:
  173.       val = 0;
  174.       break;
  175.     default:
  176.       error ("touch: Bad return code from ar_member_touch on `%s'", name);
  177.     }
  178.  
  179.   if (!arname_used)
  180.     free (arname);
  181.   free (memname);
  182.  
  183.   return val;
  184. }
  185.  
  186. /* State of an `ar_glob' run, passed to `ar_glob_match'.  */
  187.  
  188. struct ar_glob_state
  189.   {
  190.     char *arname;
  191.     char *pattern;
  192.     unsigned int size;
  193.     struct nameseq *chain;
  194.     unsigned int n;
  195.   };
  196.  
  197. /* This function is called by `ar_scan' to match one archive
  198.    element against the pattern in STATE.  */
  199.  
  200. static long int
  201. ar_glob_match (desc, mem, truncated,
  202.            hdrpos, datapos, size, date, uid, gid, mode,
  203.            state)
  204.      int desc;
  205.      char *mem;
  206.      int truncated;
  207.      long int hdrpos, datapos, size, date;
  208.      int uid, gid, mode;
  209.      struct ar_glob_state *state;
  210. {
  211.   if (fnmatch (state->pattern, mem, FNM_PATHNAME|FNM_PERIOD) == 0)
  212.     {
  213.       /* We have a match.  Add it to the chain.  */
  214.       struct nameseq *new = (struct nameseq *) xmalloc (state->size);
  215.       new->name = concat (state->arname, mem, ")");
  216.       new->next = state->chain;
  217.       state->chain = new;
  218.       ++state->n;
  219.     }
  220.  
  221.   return 0L;
  222. }
  223.  
  224. /* Alphabetic sorting function for `qsort'.  */
  225.  
  226. static int
  227. ar_glob_alphacompare (a, b)
  228.      char **a, **b;
  229. {
  230.   return strcmp (*a, *b);
  231. }
  232.  
  233. /* Return nonzero if PATTERN contains any metacharacters.
  234.    Metacharacters can be quoted with backslashes if QUOTE is nonzero.  */
  235. static int
  236. glob_pattern_p (pattern, quote)
  237.      const char *pattern;
  238.      const int quote;
  239. {
  240.   register const char *p;
  241.   int open = 0;
  242.  
  243.   for (p = pattern; *p != '\0'; ++p)
  244.     switch (*p)
  245.       {
  246.       case '?':
  247.       case '*':
  248.     return 1;
  249.  
  250.       case '\\':
  251.     if (quote)
  252.       ++p;
  253.     break;
  254.  
  255.       case '[':
  256.     open = 1;
  257.     break;
  258.  
  259.       case ']':
  260.     if (open)
  261.       return 1;
  262.     break;
  263.       }
  264.  
  265.   return 0;
  266. }
  267.  
  268. /* Glob for MEMBER_PATTERN in archive ARNAME.
  269.    Return a malloc'd chain of matching elements (or nil if none).  */
  270.  
  271. struct nameseq *
  272. ar_glob (arname, member_pattern, size)
  273.      char *arname, *member_pattern;
  274.      unsigned int size;
  275. {
  276.   struct ar_glob_state state;
  277.   char **names;
  278.   struct nameseq *n;
  279.   unsigned int i;
  280.  
  281.   if (! glob_pattern_p (member_pattern, 1))
  282.     return 0;
  283.  
  284.   /* Scan the archive for matches.
  285.      ar_glob_match will accumulate them in STATE.chain.  */
  286.   i = strlen (arname);
  287.   state.arname = (char *) alloca (i + 2);
  288.   bcopy (arname, state.arname, i);
  289.   state.arname[i] = '(';
  290.   state.arname[i + 1] = '\0';
  291.   state.pattern = member_pattern;
  292.   state.size = size;
  293.   state.chain = 0;
  294.   state.n = 0;
  295.   (void) ar_scan (arname, ar_glob_match, (long int) &state);
  296.  
  297.   if (state.chain == 0)
  298.     return 0;
  299.  
  300.   /* Now put the names into a vector for sorting.  */
  301.   names = (char **) alloca (state.n * sizeof (char *));
  302.   i = 0;
  303.   for (n = state.chain; n != 0; n = n->next)
  304.     names[i++] = n->name;
  305.  
  306.   /* Sort them alphabetically.  */
  307.   qsort ((char *) names, i, sizeof (*names), ar_glob_alphacompare);
  308.  
  309.   /* Put them back into the chain in the sorted order.  */
  310.   i = 0;
  311.   for (n = state.chain; n != 0; n = n->next)
  312.     n->name = names[i++];
  313.  
  314.   return state.chain;
  315. }
  316.  
  317. #endif    /* Not NO_ARCHIVES.  */
  318.